package day8;

import org.junit.Test;

public class Work {
	
	public static void main(String[] args) {
		String str = "";
		System.out.println(trim(str));
	}
	public static String trim(String str) {
		int start = 0;
		int end = str.length() - 1;
		
		while (start <= end && str.charAt(start) == ' ') {
			start++;
		}
		
		while (start <= end && str.charAt(end) == ' ') {
			end--;
		}
		return str.substring(start, end + 1);
	}
	
	
	
	
	// 1、实现trim方法
	@Test
	public void test1() {
		String str1 = "   java Android   ";// 6
		char[] array = str1.toCharArray();
		int start = 0;
		int end = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i] != '-') {
				start = i;
				break;
			}
		}
		for (int i = array.length - 1; i >= 0; i--) {
			if (array[i] != '-') {
				end = i + 1;
				break;
			} 
		}
		// 把字符数组转换为字符串
	
		String str2 = new String(array);
		System.out.println(str2.substring(start, end ));
	}
	
	
	@Test
	//2、实现把“I Love Java！”的字符全部转换为小写并输出到控制台。
	public void test2() {
		String str1 = "I Love Java!";
		char[] array = str1.toCharArray();
		for (int i = 0; i < array.length; i++) {
			if (array[i] >= 'A' && array[i] <= 'Z') {
//				System.out.println(array[i]);
				array[i] += 32;
//				System.out.println(array[i]);
			}
		}
		String str2 = new String(array);
		System.out.println(str2);
	}
	
	@Test
	//使用String类中的split()函数，统计出“this is my homework! I must finish it!”中单词的个数。
	//（注意：单词之间用一个空格来分隔。）
	public void test3() {
		String str1 = "this is my homework! I must finish it!";
		String[] array = str1.split(" ");
		System.out.println(array.length);
		
	}

}

